package com.nbjtjc.safe.api.danger;

import com.nbjtjc.common.validator.ValidatorUtils;
import com.nbjtjc.common.validator.group.UpdateGroup;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.danger.DangerInspectTableLabelMapper;
import com.nbjtjc.safe.model.danger.DangerInspectTableLabel;
import com.nbjtjc.safe.service.danger.DangerInspectListPublicService;
import com.nbjtjc.safe.service.danger.DangerInspectTableLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @auther guqianbin
 * @date 2018/7/27 14:05
 * 隐患检查表的标签
 **/
@RestController
@RequestMapping("/danger/inspectTableLabel")
@CrossOrigin("*")
public class DangeInspectTableLabelController {
    @Autowired
    private DangerInspectTableLabelMapper dangerInspectTableLabelMapper;

    @Autowired
    private DangerInspectTableLabelService dangerInspectTableLabelService;


    @PostMapping("/add")
    public Result add(DangerInspectTableLabel dangerInspectTableLabel){
        ValidatorUtils.validateEntity(dangerInspectTableLabel);
        return dangerInspectTableLabelService.add(dangerInspectTableLabel);
    }

    @CacheEvict(value = "dangerInspectTableLabel", key = "#dangerInspectTableLabel.companyId")
    @PostMapping("/update")
    public Result update(DangerInspectTableLabel dangerInspectTableLabel){
        ValidatorUtils.validateEntity(dangerInspectTableLabel, UpdateGroup.class);
        dangerInspectTableLabelMapper.updateByPrimaryKeySelective(dangerInspectTableLabel);
        Result result=new Result();
        result.setData(dangerInspectTableLabel);
        return result;
    }

    @CacheEvict(value = "dangerInspectTableLabel", key = "#companyId")
    @GetMapping("/delete/{id}/{companyId}")
    public Result delete(@PathVariable int id,@PathVariable int companyId){
        dangerInspectTableLabelMapper.deleteByPrimaryKey(id);
        return Result.res();
    }
    @RequestMapping(value = "/find",method = {RequestMethod.POST,RequestMethod.GET})
    public Result find(DangerInspectTableLabel dangerInspectTableLabel){
        return Result.success(dangerInspectTableLabelService.find(dangerInspectTableLabel));
    }

    @GetMapping("/getLabelStructure")
    public Result getLabelStructure(DangerInspectTableLabel dangerInspectTableLabel){

        return Result.success(dangerInspectTableLabelService.getLabelStructure(dangerInspectTableLabel.getCompanyId()));
    }

    @GetMapping("/copyLabelStructure/{fromCompanyId}/{toCompanyId}")
    public Result copyLabelStructure(@PathVariable("fromCompanyId") int fromCompanyId
            ,@PathVariable("toCompanyId") int toCompanyId){
        return Result.success(dangerInspectTableLabelService.copyLabelStructure(fromCompanyId,toCompanyId));
    }
}
